using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using UwinEducation.Domain.Entities;
using UwinEducation.Domain.IRepositories;
using UwinEducation.Infrastructure.Persistence;
using UwinEducation.Shared.Models;

namespace UwinEducation.Infrastructure.Repositories
{
    /// <summary>
    /// 教师假期仓储实现
    /// </summary>
    public class TeacherVacationRepository : Repository<TeacherVacation, Guid>, ITeacherVacationRepository
    {
        private readonly ApplicationDbContext _context;

        public TeacherVacationRepository(ApplicationDbContext context) : base(context)
        {
            _context = context;
        }

        /// <summary>
        /// 获取教师假期分页列表
        /// </summary>
        public async Task<PagedResult<TeacherVacation>> GetPagedListAsync(TeacherVacationPageRequest pageRequest)
        {
            var query = _context.Set<TeacherVacation>()
                .Include(tv => tv.Teacher)
                .AsQueryable();

            // 应用过滤条件
            if (pageRequest.TeacherId.HasValue)
            {
                query = query.Where(tv => tv.TeacherId == pageRequest.TeacherId.Value);
            }

            // 日期范围过滤
            if (pageRequest.StartDate.HasValue && pageRequest.EndDate.HasValue)
            {
                //开始时间包含在pageRequest.StartDate和pageRequest.EndDate之间
                query = query.Where(tv => tv.StartTime >= pageRequest.StartDate.Value && tv.StartTime <= pageRequest.EndDate.Value);
                query = query.Where(tv => tv.EndTime >= pageRequest.StartDate.Value && tv.EndTime <= pageRequest.EndDate.Value);

                //query = query.Where(tv => tv.StartTime >= pageRequest.StartDate.Value);
                //query = query.Where(tv => tv.StartTime >= pageRequest.StartDate.Value);
            }

            // 请假原因过滤
            if (!string.IsNullOrWhiteSpace(pageRequest.Reason))
            {
                query = query.Where(tv => tv.Reason.ToLower().Contains(pageRequest.Reason.ToLower()));
            }
            

            

            // 计算总数
            var total = await query.CountAsync();

            // 应用排序和分页
            var items = await query
                .OrderByDescending(tv => tv.StartTime)
                .Skip((pageRequest.PageIndex - 1) * pageRequest.PageSize)
                .Take(pageRequest.PageSize)
                .ToListAsync();

            return new PagedResult<TeacherVacation>
            {
                Total = total,
                Items = items
            };
        }

        /// <summary>
        /// 获取教师假期详情
        /// </summary>
        public async Task<TeacherVacation> GetDetailsByIdAsync(Guid id)
        {
            return await _context.Set<TeacherVacation>()
                .Include(tv => tv.Teacher)
                .FirstOrDefaultAsync(tv => tv.Id == id);
        }

        /// <summary>
        /// 根据教师ID获取所有假期
        /// </summary>
        public async Task<List<TeacherVacation>> GetByTeacherIdAsync(Guid teacherId)
        {
            return await _context.Set<TeacherVacation>()
                .Where(tv => tv.TeacherId == teacherId)
                .OrderByDescending(tv => tv.StartTime)
                .ToListAsync();
        }

        /// <summary>
        /// 根据日期范围获取假期
        /// </summary>
        public async Task<List<TeacherVacation>> GetByDateRangeAsync(Guid teacherId, DateTime startDate, DateTime endDate)
        {
            return await _context.Set<TeacherVacation>()
                .Where(tv => 
                    tv.TeacherId == teacherId &&
                    tv.StartTime <= endDate &&
                    tv.EndTime >= startDate
                )
                .OrderByDescending(tv => tv.StartTime)
                .ToListAsync();
        }

        /// <summary>
        /// 检查时间冲突
        /// </summary>
        public async Task<bool> HasTimeConflictAsync(Guid teacherId, DateTime startTime, DateTime endTime, Guid? excludeId = null)
        {
            var query = _context.Set<TeacherVacation>()
                .Where(tv => 
                    tv.TeacherId == teacherId &&
                    tv.StartTime <= endTime &&
                    tv.EndTime >= startTime
                );

            if (excludeId.HasValue)
            {
                query = query.Where(tv => tv.Id != excludeId.Value);
            }

            return await query.AnyAsync();
        }
    }
} 